package com.vehicle.service.Impl;

import com.vehicle.domains.bo.QueryCameraBo;
import com.vehicle.domains.entity.CameraEntity;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.mapper.CameraMapper;
import com.vehicle.service.CameraService;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CameraServiceImpl implements CameraService {

    @Resource
    private CameraMapper cameraMapper;

    @Override
    public void addCamera(CameraEntity cameraEntity) {
        try {
            cameraMapper.insertCamera(cameraEntity);
        } catch (DuplicateKeyException e) {
            throw VehicleAccessException.DATA_EXIST.newInstance("相机已存在");
        }
    }

    @Override
    public void updateCamera(CameraEntity updateEntity) {
        int i = cameraMapper.updateCameraSelective(updateEntity);
        if (i != 1) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("更新相机未更新到数据");
        }
    }

    @Override
    public CameraEntity getCameraById(Long id) {
        return cameraMapper.selectCameraById(id);
    }

    @Override
    public List<CameraEntity> queryByAssociationCameraId(Long id) {
        return cameraMapper.queryByAssociationCameraId(id);
    }

    @Override
    public void deleteCamera(Long id) {
        int i = cameraMapper.deleteCameraById(id);
        if (i != 1) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("删除相机未删除到数据");
        }
    }

    @Override
    public List<CameraEntity> selectCameraList(QueryCameraBo queryCameraBo) {
        return cameraMapper.selectCameraList(queryCameraBo);
    }

    @Override
    public void unBindCamera(Long  id) {
        int i = cameraMapper.unBindCamera(id);
        if (i != 1) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("解除绑定相机未更新到数据");
        }
    }

    @Override
    public CameraEntity getCameraByIp(String ip) {
        return cameraMapper.getCameraByIp(ip);
    }
}
